n=int(input())
qwr=[]
qwr_pr=[0]
count=0
if 999<n<10000:
  s=str(n)
  n1=int(s[0])
  n2=int(s[0])+int(s[1])
  n3=int(s[0])+int(s[1])+int(s[2])
  n4=int(s[0])+int(s[1])+int(s[2])+int(s[3])
  n5=int(s[1])
  n6=int(s[1])+int(s[2])
  n7=int(s[1])+int(s[2])+int(s[3])
  n8=int(s[2])
  n9=int(s[2])+int(s[3])
  n10=int(s[3])
  qwr=[n1, n2, n3, n4, n5, n6, n7, n8, n9, n10]
  for i in qwr:
    for d in range(1, 5001):
      if i % d == 0:
        count+=1
    if count == 2:
      qwr_pr.append(i)
    count=0
  print(max(qwr_pr))
else:
  print('0')
      
